package com.iknow.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import com.iknow.IKnow;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageUtil
{
  private static final File coverImagePath = new File(SystemUtil.fillSDPath("cover" + File.separator + "image"));
  private static final String tag = "com.iknow.util.ImageUtil";

  public static byte[] BitmapToBytes(Bitmap paramBitmap)
  {
    byte[] arrayOfByte;
    if (paramBitmap == null)
      arrayOfByte = null;
    while (true)
    {
      return arrayOfByte;
      ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
      paramBitmap.compress(Bitmap.CompressFormat.PNG, 100, localByteArrayOutputStream);
      arrayOfByte = localByteArrayOutputStream.toByteArray();
    }
  }

  public static Bitmap BytesToBitmap(byte[] paramArrayOfByte)
  {
    Bitmap localBitmap;
    if ((paramArrayOfByte != null) && (paramArrayOfByte.length != 0))
      localBitmap = BitmapFactory.decodeByteArray(paramArrayOfByte, 0, paramArrayOfByte.length);
    while (true)
    {
      return localBitmap;
      localBitmap = null;
    }
  }

  public static Bitmap circumgyrateImage(Bitmap paramBitmap, int paramInt)
  {
    Matrix localMatrix = new Matrix();
    localMatrix.postRotate(paramInt);
    return Bitmap.createBitmap(paramBitmap, 0, 0, paramBitmap.getWidth(), paramBitmap.getHeight(), localMatrix, true);
  }

  public static File createCacheFile(Context paramContext, Bitmap paramBitmap)
    throws IOException
  {
    File localFile = File.createTempFile("bm_", ".png", paramContext.getCacheDir());
    createImageFile(paramContext, localFile, paramBitmap);
    return localFile;
  }

  public static File createCoverImage(Context paramContext, Bitmap paramBitmap)
    throws IOException
  {
    if (!(coverImagePath.exists()))
      coverImagePath.mkdirs();
    File localFile = File.createTempFile("cover_", ".png", coverImagePath);
    if (localFile != null)
      createImageFile(paramContext, localFile, paramBitmap);
    return localFile;
  }

  public static void createImageFile(Context paramContext, File paramFile, Bitmap paramBitmap)
    throws IOException
  {
    FileOutputStream localFileOutputStream;
    File localFile = paramFile.getParentFile();
    if ((localFile != null) && (!(localFile.exists())))
      localFile.mkdirs();
    paramFile.delete();
    paramFile.createNewFile();
    Object localObject1 = null;
    try
    {
      localFileOutputStream = new FileOutputStream(paramFile);
    }
    finally
    {
      try
      {
        localFileOutputStream.write(BitmapToBytes(paramBitmap));
        localFileOutputStream.flush();
        if (localFileOutputStream != null)
          localFileOutputStream.close();
        return;
        localObject2 = finally;
        if (localObject1 != null)
          localObject1.close();
        throw localObject2;
      }
      finally
      {
        localObject1 = localFileOutputStream;
      }
    }
  }

  public static Bitmap getBitmap(Context paramContext, File paramFile)
  {
    Object localObject1;
    if ((paramFile == null) || (!(paramFile.exists())))
    {
      localObject1 = null;
      return localObject1;
    }
    Object localObject2 = null;
    try
    {
      Bitmap localBitmap = BitmapFactory.decodeFile(paramFile.getPath());
      localObject2 = localBitmap;
      localObject1 = localObject2;
    }
    catch (Exception localException)
    {
      while (true)
        Loger.e("com.iknow.util.ImageUtil", StringUtil.objToString(localException));
    }
  }

  public static Bitmap getDefaultBitmap(String paramString)
  {
    Object localObject2;
    if (StringUtil.isEmpty(paramString))
      localObject2 = null;
    while (true)
    {
      int i;
      int j;
      return localObject2;
      Object localObject1 = null;
      String[] arrayOfString1 = paramString.split("loc://");
      AssetManager localAssetManager = IKnow.mContext.getAssets();
      new StringBuilder("assets").append(File.separator).append("image").toString();
      label144: 
      do
        try
        {
          String[] arrayOfString2 = localAssetManager.list("image");
          i = arrayOfString2.length;
          j = 0;
          break label144:
          String str = arrayOfString2[j];
          if (str.equalsIgnoreCase(arrayOfString1[1]))
          {
            Bitmap localBitmap = BitmapFactory.decodeStream(localAssetManager.open("image" + File.separator + str));
            localObject1 = localBitmap;
          }
          ++j;
        }
        catch (IOException localIOException)
        {
          while (true)
          {
            localIOException.printStackTrace();
            localObject2 = null;
          }
        }
      while (j < i);
      localObject2 = localObject1;
    }
  }

  public static Bitmap resizeImage(Bitmap paramBitmap, int paramInt1, int paramInt2)
  {
    float f1 = paramInt1 / paramBitmap.getWidth();
    float f2 = paramInt2 / paramBitmap.getHeight();
    Matrix localMatrix = new Matrix();
    localMatrix.postScale(f1, f2);
    Bitmap localBitmap = Bitmap.createBitmap(paramBitmap, 0, 0, paramBitmap.getWidth(), paramBitmap.getHeight(), localMatrix, true);
    if (!(paramBitmap.isRecycled()))
      paramBitmap.recycle();
    return localBitmap;
  }

  public static void setSelectImage(Activity paramActivity, int paramInt)
  {
    Intent localIntent = new Intent("android.intent.action.GET_CONTENT");
    localIntent.setType("image/*");
    paramActivity.startActivityForResult(Intent.createChooser(localIntent, null), paramInt);
  }
}